package com.test.exam.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.test.exam.domain.Student;
import com.test.exam.service.BanService;
import com.test.exam.service.StudentService;


@Controller
public class StudentController {
	@Autowired
	private StudentService studentService;
	@Autowired
	private BanService banService;	

	
	//현재 페이지 학생 목록
	@RequestMapping(value="/admin/student/getStudentsByCurrentPage", 
			method=RequestMethod.GET)
	public String getStudentsByCurrentPage(
			@RequestParam(defaultValue="0") int banIdx,
			@RequestParam(defaultValue="1") int currentPage,
			Model model){
		model.addAttribute(
				"studentList", 
				studentService.getStudentsByCurrentPage(banIdx, currentPage));
		
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("endPage", studentService.getEndPage(banIdx));
		model.addAttribute("banIdx", banIdx);
		
		model.addAttribute("banList",banService.getAllBans());
		return "/admin/student/studentList";
	}
	
	
	// 학생 입력
	@RequestMapping(value="/admin/student/addStudent",
					method=RequestMethod.POST)
	public String addStudent(Student student){
	//	System.out.println(student.getStudentName());
		studentService.addStudent(student);
		return "redirect:/admin/student/getStudentsByCurrentPage";
	}
	 
	// 학생 입력 폼
	@RequestMapping(value="/admin/student/addStudentForm", 
					method=RequestMethod.GET)
	public String addStudentForm(Model model){
		model.addAttribute("banList",banService.getAllBans());
		return "/admin/student/addStudentForm";
	}
	
}











